package com.moyq5.permit.center.web.jackson.filter;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.permit.center.bean.entity.Shop;
import com.moyq5.permit.center.bean.entity.ShopRole;
import com.moyq5.permit.center.bean.entity.SysRole;

/**
 * @author Moyq5's MvcMaker Tools
 */
public abstract class ShopRoleFilter {

	public static FilterProvider get(String filter) {
		if (StringUtils.isEmpty(filter)) {
			return get();
		}
		try {
			Method method = ShopRoleFilter.class.getMethod("getWith" + String.valueOf(filter.charAt(0)).toUpperCase() + filter.substring(1));
			return (FilterProvider)method.invoke(null);
		} catch (Exception e) {
			return get();
		}
	}
	
	public static Set<String> baseFields() {
		final Set<String> shopRole = new HashSet<String>();
		shopRole.add("id");
		return shopRole;
	}

	public static FilterProvider getId() {
		final Set<String> shopRole = new HashSet<String>();
		shopRole.add("id");
		
		return new SimpleFilterProvider().addFilter(
				ShopRole.class.getSimpleName(),
				SimpleBeanPropertyFilter.filterOutAllExcept(shopRole));
	}

	public static FilterProvider get() {
		return new SimpleFilterProvider().addFilter(
				ShopRole.class.getSimpleName(),
				SimpleBeanPropertyFilter.filterOutAllExcept(baseFields()));
	}

	public static FilterProvider getWithRole() {
		final Set<String> shopRole = baseFields();
		shopRole.add("role");
		final Set<String> role = SysRoleFilter.baseFields();

		return new SimpleFilterProvider()
				.addFilter(ShopRole.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(shopRole))
				.addFilter(SysRole.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(role));
	}

	public static FilterProvider getWithShopAndRole() {
		final Set<String> merchRole = baseFields();
		merchRole.add("shop");
		merchRole.add("role");
		final Set<String> role = SysRoleFilter.baseFields();
		final Set<String> shop = ShopFilter.baseFields();

		return new SimpleFilterProvider()
				.addFilter(ShopRole.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(merchRole))
				.addFilter(Shop.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(shop))
				.addFilter(SysRole.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept(role));
	}

}
